package com.example.lcode.binary;

import java.util.Stack;

public class Question456 {

    public boolean find132pattern(int[] nums) {
        if (nums.length <= 2) {
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        stack.add(nums[nums.length - 1]);
        int min = Integer.MIN_VALUE;
        for (int i = nums.length - 2; i >= 0 ; i--) {
            if (min > nums[i]) return true;
            while (!stack.isEmpty() && stack.peek() < nums[i]) {
                min = Math.max(stack.pop(), min);
            }
            stack.add(nums[i]);
        }
        return false;
    }


    public static void main(String[] args) {
        Question456 question456 = new Question456();
        int []arr = {3,1,4,2};
        boolean pattern = question456.find132pattern(arr);
        System.out.println(pattern);
    }

}
